<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>05_猜数字</title>
</head>
<body>
<h3>已经生成一个随机数</h3>
<input type="text" id="num" placeholder="请输入你猜的数字">
<button onclick="fma()">点我验证</button>
<br>
<h4>结果为:<span id="re"></span></h4>

</body>

<script>
    let preNu = parseInt(Math.random()*100+1);
    console.log(preNu);
    var  count=0;
    function fma() {

        let num = parseInt(document.querySelector('#num').value);

        count++;
        // !/^\d+$/.num
        if(isNaN(num)){
            alert("请输入数字");
        }else if(num>preNu){
            document.querySelector('#re').innerHTML="数字大了";
        }else if(num<preNu){
            document.querySelector('#re').innerHTML="数字小了";
        }else {
            document.querySelector('#re').innerHTML="恭喜你,用了"+count+"次猜对了";
            count = 0;
            preNu = parseInt(Math.random()*100+1);
            console.log(preNu);
        }
        document.querySelector('#num').value='';

    }
</script>
</html>